class Solution {
    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println(s.multiply("2", "3"));
    }

    public String multiply(String num1, String num2) {
        /**
         * 高精度乘法*/
        // 1 预处理
        // -逆序字符串（以对应从个位开始处理）
        StringBuilder s1 = new StringBuilder(num1).reverse();
        StringBuilder s2 = new StringBuilder(num2).reverse();
        int m = s1.length();
        int n = s2.length();
        int[] arr = new int[m+n-1];

        // 2 无进位相乘
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                arr[i+j] += ((s1.charAt(i) - '0') * (s2.charAt(j) - '0'));
            }
        }

        // 3 处理进位
        StringBuilder answer = new StringBuilder();
        long carry = 0;
        for(int i = 0; i < m+n-1; i++) {
            carry += arr[i];
            answer.append(carry % 10);
            carry /= 10;
        }
        // -逆序（因为在预处理时将字符逆序，故所计算的结果也是逆序）
        answer.reverse();
        answer.insert(0, carry);

        // 4 处理前置0
        int k = 0;
        while(k < answer.length() && answer.charAt(k) == '0') {
            k ++;
        }
        if(k == answer.length()) {
            return "0";
        } else {
            return answer.substring(k);
        }
    }
}